// 用户的禁用、启用、重置密码
import { UserDataProps } from "~/server/models/user";
import bcrypt from "bcrypt";
export default defineEventHandler(async (event) => {
  const id = getRouterParam(event, "id");
  const query = getQuery(event);
  const { handleType, newPassword } = query;
  let newData: Partial<UserDataProps> = {};
  const config = useRuntimeConfig();
  switch (handleType) {
    case "disable":
      newData.disabled = true;
      break;
    case "enable":
      newData.disabled = false;
      break;
    case "resetPassword":
      newData = {
        password: await bcrypt.hash(
          config.bcrypt.defaultPassword,
          config.bcrypt.saltRounds
        ),
      };
      break;
    case "editPassword":
      if (newPassword) {
        newData = {
          password: await bcrypt.hash(
            String(newPassword),
            config.bcrypt.saltRounds
          ),
        };
      }
      break;
    default:
      break;
  }
  const newUser = UserSchema.findByIdAndUpdate(id, newData, { new: true });
  return newUser;
});
